<template>
  <el-dialog
    width="800"
    title="参与人数"
    v-model="visible"
    draggable
    class="dialog"
    @close="closeDialog"
  >
    <el-form v-show="showSearch" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入用户"
          @keyup.enter="handleSearch(true)"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="phone">
        <el-input
          v-model.trim="queryParams.phone"
          placeholder="请输入用户账号"
          @keyup.enter="handleSearch(true)"
        />
      </el-form-item>

      <el-form-item label="参与点位" prop="positionId">
        <el-select
          style="width: 240px"
          v-model="queryParams.positionId"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in positionOptions"
            :key="item.positionId"
            :label="item.positonName"
            :value="item.positionId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="&nbsp;">
        <el-button @click="handleSearch(false)">重置</el-button>
        <el-button type="primary" @click="handleSearch(true)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column type="index" :index="indexAdd" label="编号" width="50" align="center" />
      <el-table-column label="用户" align="center" prop="name" />
      <el-table-column label="用户账号" align="center" prop="phone" />
      <el-table-column label="累计打卡" align="center" prop="totalSignIn">
        <template #default="{ row }">
          <span>{{ row.totalSignIn ?? 0 }}单 </span>
        </template>
      </el-table-column>
      <el-table-column label="参与点位" align="center" prop="positionName" />
      <el-table-column label="获得奖励" align="center" prop="status">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleDetails(row)"> 详情 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-right: 50px; position: relative">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <participantsDetails ref="participantsDetailsRef" />
  </el-dialog>
</template>
<script setup>
import { getPositionListApi } from '@/api/marketing/activity'
import participantsDetails from './participantsDetails'
import { getParticipationApi } from '@/api/marketing/punchCard'

const queryRef = ref()
const tableList = ref([])
const loading = ref(false)
const showSearch = ref(true)
const total = ref(0)
const visible = ref(false)
const positionOptions = ref([])
const queryParams = ref({
  phone: null,
  name: null,
  positionId: null,
  activitySignInsId: null,
  pageNum: 1,
  pageSize: 10,
  cityName: null
})
const participantsDetailsRef = ref(null)
function handleSearch(type) {
  if (type) {
    // 搜索
    queryParams.value.pageNum = 1
    getList()
  } else {
    // 重置
    queryRef.value.resetFields()

    handleSearch(true)
  }
}

function indexAdd(index) {
  const { pageNum, pageSize } = queryParams.value
  return index + 1 + (pageNum - 1) * pageSize
}
function handleDetails(row) {
  participantsDetailsRef.value.init(
    {
      ...row,
      activitySignInsId: queryParams.value.activitySignInsId
    },
    queryParams.value.cityName
  )
}
function getList() {
  loading.value = true
  getParticipationApi(queryParams.value)
    .then((res) => {
      tableList.value = res?.data?.rows ?? []
      total.value = res?.data?.total ?? 0
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}
function getPositions() {
  getPositionListApi({ cityName: queryParams.value.cityName }).then((res) => {
    positionOptions.value = res?.data || []
  })
}
function init(row, cityName) {
  queryParams.value.activitySignInsId = row.id
  queryParams.value.cityName = cityName
  visible.value = true
  getList()
  getPositions()
}
function closeDialog() {
  queryRef.value.resetFields()
  visible.value = false
}

defineExpose({
  init
})
</script>
<style lang="scss" scoped></style>
